package example12_6;

import java.util.Stack;
public class WriteStack extends Thread
{
    private Stack<Integer> stack;
    private int count=0;

    public WriteStack(Stack<Integer> stack, String name) {
        super(name);
        this.stack = stack;
    }

    @Override
    public void run() {
        
        System.out.println(Thread.currentThread().getName()+"开始启动了......");
        while(count<10)
        {
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            synchronized(stack)
            {
                if(this.stack.size()==3)
                {
                    try {
                        stack.wait();
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }
                else
                {
                    this.stack.push(count);
                    System.out.println(Thread.currentThread().getName()+" 进行了写操作，写入数据："+count);
                    count++;
                    if(stack.size()==3||count==10)
                    stack.notify();
                }
            }
        }
    }


}

